﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntdUI;

//
// 摘要:
//     LabelTime 时间文本
//
// 言论：
//     显示时间文本。
[Description("LabelTime 时间文本")]
[ToolboxItem(true)]
[DefaultProperty("ShowTime")]
public class LabelTimeEx : IControl
{
    private bool showTime = true;

    private string show_tmp = "";

    private readonly StringFormat s_f_l = Helper.SF_NoWrap(StringAlignment.Center, StringAlignment.Far);

    private readonly StringFormat s_f_r1 = Helper.SF_NoWrap(StringAlignment.Far, StringAlignment.Near);

    private readonly StringFormat s_f_r2 = Helper.SF_NoWrap(StringAlignment.Near, StringAlignment.Near);

    [Description("外观")]
    [Category("是否显示秒")]
    [DefaultValue(true)]
    public bool ShowTime
    {
        get
        {
            return showTime;
        }
        set
        {
            if (showTime != value)
            {
                showTime = value;
                Invalidate();
            }
        }
    }

    public LabelTimeEx()
    {
        Thread thread = new Thread(TaskLong);
        thread.IsBackground = true;
        thread.Start();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle clientRectangle = base.ClientRectangle;
        Canvas canvas = e.Graphics.High();
        string[] array = GTime();
        show_tmp = string.Join("", array);
        using (SolidBrush brush = new SolidBrush(ForeColor.rgba(0.8f)))
        {
            using Font font = new Font(Font.FontFamily, (float)clientRectangle.Height * 0.72f, FontStyle.Bold, GraphicsUnit.Pixel);
            Size size = canvas.MeasureString(array[0], font, 10000, s_f_l);
            Rectangle rect = new Rectangle(clientRectangle.X, clientRectangle.Y, size.Width, clientRectangle.Height);
            canvas.String(array[1], font, ForeColor, rect, s_f_l);
            int num = rect.Height / 2;
            int num2 = rect.Width + (int)((float)size.Height * 0.24f);
            int width = clientRectangle.Width - num2;
            using Font font2 = new Font(Font.FontFamily, font.Size * 0.36f, GraphicsUnit.Pixel);
            //canvas.String(array[2], font2, brush, new Rectangle(clientRectangle.X + num2, clientRectangle.Y, width, num), s_f_r1);
            //canvas.String(array[3], font2, brush, new Rectangle(clientRectangle.X + num2, clientRectangle.Y + num, width, num), s_f_r2);
        }

        this.PaintBadge(canvas);
        base.OnPaint(e);
    }

    private void TaskLong()
    {
        while (!base.IsDisposed)
        {
            Thread.Sleep(ShowTime ? 1000 : 10000);
            if (!base.IsDisposed)
            {
                string[] value = GTime();
                if (string.Join("", value) != show_tmp)
                {
                    Invalidate();
                }

                continue;
            }

            break;
        }
    }

    private string[] GTime()
    {
        DateTime now = DateTime.Now;
        if (!ShowTime)
        {
            return new string[4]
            {
                "24:59",
                now.ToString("HH:mm"),
                now.ToString("MM-dd"),
                now.ToString("dddd")
            };
        }

        return new string[4]
        {
            "24:59:59",
            now.ToString("HH:mm:ss"),
            now.ToString("MM-dd"),
            now.ToString("dddd")
        };
    }
}
